
import { _decorator, Component, Node, TiledMap, Vec3, BoxCollider2D, Vec2, RigidBody2D, ERaycast2DType, ERigidBody2DType, Size, CircleCollider2D, PolygonCollider2D, Prefab, instantiate } from 'cc';
import { EnemyPeople } from './animy/EnemyPeople';
import { SteelGun } from './animy/SteelGun';
import { ColliderTag } from './Globle';
const { ccclass, property } = _decorator;

@ccclass('TiledMapControl')
export class TiledMapControl extends Component {
    @property(Node) gameScene: Node
    //钢炮
    @property(Prefab) steel_gun: Prefab
    //敌人
    @property(Prefab) enemy_people: Prefab
    private map: TiledMap;

    start() {
        this.map = this.getComponent(TiledMap);
        this.initCllider();
        this.initSteelGun();
        this.initEnemyPeople();

    }

    private initCllider() {
        let node = this.map.getLayer("background").node;
        let object = this.map.getObjectGroup("collider")

        for (let obj of object.getObjects()) {
            let collider: BoxCollider2D | CircleCollider2D | PolygonCollider2D;
            switch (obj.type) {
                case 0:
                    collider = node.addComponent(BoxCollider2D);
                    collider.offset = new Vec2(obj.offset.x + obj.width / 2, -(obj.offset.y + obj.height / 2));
                    collider.size = new Size(obj.width, obj.height);
                    collider.apply();
                    break;
                case 1:
                    collider = node.addComponent(CircleCollider2D);
                    collider.offset = new Vec2(obj.offset.x + obj.width / 2, -(obj.offset.y + obj.height / 2));
                    collider.radius = obj.width / 2;
                    collider.apply();
                    break;
                case 2:
                    collider = node.addComponent(PolygonCollider2D);
                    collider.offset = new Vec2(obj.offset.x + obj.width / 2, -(obj.offset.y + obj.height / 2));
                    collider.points = obj.points.map(v => new Vec2(v.x, v.y));
                    collider.apply();
                    break;


            }
            if (obj.name == 'enemy_wall') collider.tag = ColliderTag.enemyWall;
            if (obj.name == 'wall2') collider.tag = ColliderTag.wall2;

        }
    }

    //初始化钢炮
    private initSteelGun() {
        let object = this.map.getObjectGroup("steel_gun");
        for (let obj of object.getObjects()) {
            let steel_gun_node = instantiate(this.steel_gun);
            let Steel_gun = steel_gun_node.getComponent(SteelGun);
            steel_gun_node.position = new Vec3(obj.offset.x + obj.width / 2, -obj.offset.y - obj.height / 2)
            this.gameScene.insertChild(steel_gun_node, 2);
        }
    }

    //初始化敌人
    private initEnemyPeople() {
        let object = this.map.getObjectGroup("enemy_people");
        for (let obj of object.getObjects()) {
            let enemy_people_node = instantiate(this.enemy_people);
            let Enemy_eople = enemy_people_node.getComponent(EnemyPeople);
            Enemy_eople.type = obj.name;
            enemy_people_node.position = new Vec3(obj.offset.x + obj.width / 2, -obj.offset.y - obj.height / 2)
            this.gameScene.insertChild(enemy_people_node, 2);
        }
    }

    update(deltaTime: number) {
        this.map.node.position = new Vec3();
    }
}
